
from whiff.middleware import misc
from whiff import whiffenv
from whiff import resolver
import math

class radiusCalculations(misc.utility):
    def __init__(self, page):
        self.page = page
    def __call__(self, env, start_response):
        env = resolver.process_cgi(env, parse_cgi=True)
        radiusString = whiffenv.cgiGet(env, "Radius")
        try:
            radius = float(radiusString.strip())
        except ValueError:
            env["errorFlag"] = "ParseError"
        else:
            if radius<0:
                env["errorFlag"] = "NegativeRadius"
            else:
                env["CircleArea"] = math.pi * (radius**2)
                env["CircleCircumference"] = 2 * math.pi * radius
                env["SphereVolume"] = 4 * math.pi * (radius**3) / 3
        its = env.items()
        its.sort()
        for x in its:
            print x
        return self.deliver_page(self.page, env, start_response)

__middleware__ = radiusCalculations
